define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'pms/pmsorderpayment/index' + location.search,
                    add_url: 'pms/pmsorderpayment/add',
                    multi_url: 'pms/pmsorderpayment/multi',
                    table: 'pms_order_payment',
                }
            });

            var table = $("#table");

            //在普通搜索渲染后
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='pay_type']", form).addClass("selectpage").data("source", "pms/pmsconfig/selectpage").data("primaryKey", "id").data("field", "name").attr('data-params', '{"key":"pay_type"}');

                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                searchFormVisible: true,
                search: false,
                columns: [
                    [
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        {field: 'pay_type', title: __('Pay_type')},
                        {field: 'money', title: __('Money'), operate:'BETWEEN'},
                        {field: 'addons', title: __('Addons'), operate: false, formatter: Table.api.formatter.images},
                        {field: 'state', title: __('状态'), searchList: {"0":__('正常'),"2":__('已作废')}, formatter: Table.api.formatter.status},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'last_editor_nickname', title: __('Last_editor'), operate: false},
                        // {field: 'checker', title: __('Checker'), operate: false},
                        // {field: 'check_time', title: __('Check_time'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate, buttons: [{
                                name: '收款详情',
                                title: __('收款详情'),
                                classname: 'btn btn-xs btn-info btn-dialog',
                                text: '收款详情',
                                icon: 'fa fa-file-text-o',
                                extend: 'data-area=\'["80%","80%"]\'',
                                url: 'pms/pmsorderpayment/detail'
                            }]}
                    ]
                ]
            });

            //双击编辑
            $(table).on("dblclick", "tr", function(e){
                e.preventDefault();
                $(this).find('.btn-dialog').click();
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            setTimeout(function(){
                $('.plupload-preview a.btn-trash').remove();
            }, 50);

            $('#js_cancel').click(function(){
                $('#editModal').modal('show');
            });


            $('#editModal .btn-primary').on('click', function(e){
                var that = $(this);
                var id = $('#c-id').val();
                var remarks = $('#c-cancelRemarks').val();

                if (remarks.length == 0)
                {
                    Layer.msg('请录入作废理由')
                }
                else
                {
                    Layer.confirm('确定作废这一记录', {
                        icon: 3,
                        title: '提示'
                    }, function (index) {
                        Backend.api.ajax({
                            url: 'pms/Pmsroomstatus/ajax_cancel_payment',
                            data: {id: id, remarks:remarks}
                        }, function (d) {
                            Layer.close(index);
                            setTimeout(function(){
                                window.location.reload();
                            }, 1000);
                        });
                    });
                }
            });

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});